package com.cjyd.hrjb.convert;

import com.baomidou.mybatisplus.core.enums.IEnum;
import com.cjyd.hrjb.model.enums.BaseEnum;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created fei wo by 2020/4/7
 */
public class EnumSerializer extends JsonSerializer<BaseEnum> {

    @Override
    public void serialize(BaseEnum value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (value == null) {
            gen.writeNull();
            return;
        }

        Map<String,Object> obj = new HashMap<>();
        obj.put("val", value.getValue());
        obj.put("desc", value.getDesc());
        gen.writeObject(obj);

    }

    @Override
    public Class<BaseEnum> handledType() {
        return BaseEnum.class;
    }
}
